using Microsoft.AspNetCore.Mvc;
using UniversalAdminSystem.Application.Common.Results;
using UniversalAdminSystem.Application.UserManagement.Dtos;
using UniversalAdminSystem.Application.UserManagement.Interface;
using UniversalAdminSystem.Api.Attributes;

namespace UniversalAdminSystem.Api.Controllers;

/// <summary>
/// 用户管理控制器
/// </summary>
[ApiController]
[Route("api/user")]
public class UserManagementController : ControllerBase
{
    private readonly IUserManagementAppService _userRepoService;

    public UserManagementController(IUserManagementAppService userService)
    {
        _userRepoService = userService;
    }

    [HttpGet]
    [RequirePermission("user:Read")]
    public async Task<IActionResult> GetUsersAsync()
    {
        var result = await _userRepoService.GetUsersAsync();
        
        if (result.IsSuccess)
        {
            return Ok(result);
        }
        else
        {
            return BadRequest(result);
        }
    }

    [HttpPost("create")]
    [RequirePermission("user:Create")]
    public async Task<IActionResult> CreateUserAsync([FromBody] UserCreateDto userCreate)
    {
        var result = await _userRepoService.CreateUserAsync(userCreate);
        
        if (result.IsSuccess)
        {
            return Ok(result);
        }
        else
        {
            return BadRequest(result);
        }
    }

    [HttpDelete("{id}")]
    [RequirePermission("user:Delete")]
    public async Task<IActionResult> DeleteUserAsync(Guid id)
    {
        var result = await _userRepoService.DeleteUserAsync(id);
        
        if (result.IsSuccess)
        {
            return Ok(result);
        }
        else
        {
            return BadRequest(result);
        }
    }

    [HttpGet("{id}")]
    [RequirePermission("user:Read")]
    public async Task<IActionResult> GetUserByIdAsync(Guid id)
    {
        var result = await _userRepoService.GetUserByIdAsync(id);
        
        if (result.IsSuccess)
        {
            return Ok(result);
        }
        else
        {
            return BadRequest(result);
        }
    }

    [HttpPut("{id}")]
    [RequirePermission("user:Update")]
    public async Task<IActionResult> UpdateUserAsync(Guid id, [FromBody] UserUpdateDto updateDto)
    {
        var result = await _userRepoService.UpdateUserAsync(id, updateDto);
        
        if (result.IsSuccess)
        {
            return Ok(result);
        }
        else
        {
            return BadRequest(result);
        }
    }

    // 已废弃（批量获取角色）
    // [HttpPost("{id}/role")]
    // [RequirePermission("user:Update")]
    // public async Task<IActionResult> AssignRoleAsync(Guid id, [FromBody] List<Guid> roleIds)
    // {
    //     var result = await _userRepoService.AssignRoleAsync(id, roleIds);

    //     if (result.IsSuccess)
    //     {
    //         return Ok(result);
    //     }
    //     else
    //     {
    //         return BadRequest(result);
    //     }
    // }

    /// <summary>
    /// 移除用户角色
    /// </summary>
    [HttpDelete("{id}/roles/{roleId}")]
    [RequirePermission("user:Update")]
    public async Task<IActionResult> RemoveRoleAsync(Guid id, Guid roleId)
    {
        var result = await _userRepoService.RemoveRoleAsync(id, roleId);
        
        if (result.IsSuccess)
        {
            return Ok(result);
        }
        else
        {
            return BadRequest(result);
        }
    }

    /// <summary>
    /// 获取用户的所有权限
    /// </summary>
    [HttpGet("{id}/permissions")]
    [RequirePermission("user:Read")]
    public async Task<IActionResult> GetUserPermissionsAsync(Guid id)
    {
        var result = await _userRepoService.GetUserPermissionsAsync(id);
        
        if (result.IsSuccess)
        {
            return Ok(result);
        }
        else
        {
            return BadRequest(result);
        }
    }

    /// <summary>
    /// 检查用户是否有指定权限
    /// </summary>
    [HttpPost("{id}/permissions/check")]
    [RequirePermission("user:Read")]
    public async Task<IActionResult> CheckUserPermissionAsync(Guid id, [FromBody] string permissionCode)
    {
        var result = await _userRepoService.CheckUserPermissionAsync(id, permissionCode);
        
        if (result.IsSuccess)
        {
            return Ok(result);
        }
        else
        {
            return BadRequest(result);
        }
    }
}
